class Solution {
public:
    int countOfOne(int n)
    {
        int count = 0;
        while (n)
        {
            n &= n - 1;
            count++;
        }
        return count;
    }

    bool canSortArray(vector<int>& nums) {
        int n = nums.size();
        for (int i = 0; i < n - 1; i++)
        {
            bool flag = true;
            for (int j = 0; j < n - 1 - i; j++)
            {
                if (nums[j] > nums[j + 1])
                {
                    flag = false;
                    if (countOfOne(nums[j]) == countOfOne(nums[j + 1]))
                        swap(nums[j], nums[j + 1]);
                    else
                        return false;
                }
            }
            if (flag) return true;
        }
        return true;
    }
};